package com.yjhb.android.sbz.mvp.blacklist

import com.yjhb.android.sbz.mvp.ConventionPresenter2
import com.tencent.imsdk.v2.*

class BlacklistPresenter(blacklistView: BlacklistView) : ConventionPresenter2<BlacklistView>(blacklistView) {


    /**
     * 获取黑名单
     */
    fun getBlackList() {
        V2TIMManager.getFriendshipManager().getBlackList(object : V2TIMValueCallback<List<V2TIMFriendInfo>?> {
            override fun onError(code: Int, desc: String) {
                view?.onListLoadFail(code, desc)
            }

            override fun onSuccess(v2TIMFriendInfos: List<V2TIMFriendInfo>?) {
                v2TIMFriendInfos?.let { list ->
                    val friends = mutableListOf<V2TIMUserFullInfo>()
                    for (i in list) {
                        friends.add(i.userProfile)
                    }
                    view?.onBlackList(friends)
                }
            }
        })
    }


    /**
     * 加入黑名单
     */
    fun addBlack(id: String) {
        view?.onWait()
        val ids = mutableListOf(id)
        V2TIMManager.getFriendshipManager().addToBlackList(ids, object : V2TIMValueCallback<List<V2TIMFriendOperationResult?>?> {
            override fun onError(code: Int, desc: String) {
                view?.onFailure(code, desc)
            }

            override fun onSuccess(v2TIMFriendOperationResults: List<V2TIMFriendOperationResult?>?) {
                view?.onSuccess(1)
            }
        })
    }

    /**
     * 从黑名单移除
     */
    fun deleteBlack(id: String) {
        view?.onWait()
        val ids = mutableListOf(id)
        V2TIMManager.getFriendshipManager().deleteFromBlackList(ids, object : V2TIMValueCallback<List<V2TIMFriendOperationResult?>?> {
            override fun onError(code: Int, desc: String) {
                view?.onFailure(code, desc)
            }

            override fun onSuccess(v2TIMFriendOperationResults: List<V2TIMFriendOperationResult?>?) {
                view?.onSuccess(2)
            }
        })
    }

    /**
     * 好友信息
     */
    fun getFriendInfo(id: String) {
        val ids = mutableListOf(id)
        V2TIMManager.getInstance().getUsersInfo(ids, object : V2TIMValueCallback<MutableList<V2TIMUserFullInfo>?> {
            override fun onSuccess(t: MutableList<V2TIMUserFullInfo>?) {
                t?.let {
                    if (it.size > 0)
                        view?.onFriendInfo(it[0])
                    else
                        onError(1002, "没有找到该联系人！")
                }
            }

            override fun onError(code: Int, desc: String?) {
                view?.onFailure(code, desc)
            }
        })
    }


}